package com.dstz.bpm.engine.parser;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.core.util.AppUtil;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.model.nodedef.impl.SubProcessNodeDef;
import com.dstz.bpm.engine.model.DefaultBpmProcessDef;
import com.dstz.bpm.engine.parser.flow.AbsFlowParse;
import com.dstz.bpm.engine.parser.node.AbsNodeParse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BpmProcessDefParser {
   private static List<AbsFlowParse> bv;
   private static List<AbsNodeParse> bw;

   public static void a(DefaultBpmProcessDef bpmProcessDef, JSONObject bpmDefSetting) {
      JSONObject flowConf = bpmDefSetting.getJSONObject("flow");
      Iterator var3 = getFlowParsers().iterator();

      while(var3.hasNext()) {
         AbsFlowParse flowParser = (AbsFlowParse)var3.next();
         flowParser.parse(bpmProcessDef, flowConf);
      }

      JSONObject nodeMap = bpmDefSetting.getJSONObject("nodeMap");
      Iterator var10 = bpmProcessDef.getBpmnNodeDefs().iterator();

      while(var10.hasNext()) {
         BpmNodeDef nodeDef = (BpmNodeDef)var10.next();
         JSONObject nodeConfig = nodeMap.getJSONObject(nodeDef.getNodeId());
         Iterator var7 = getNodeParsers().iterator();

         while(var7.hasNext()) {
            AbsNodeParse nodeParser = (AbsNodeParse)var7.next();
            if (nodeParser.a(nodeDef)) {
               nodeParser.parse(nodeDef, nodeConfig);
            }
         }

         if (nodeDef instanceof SubProcessNodeDef) {
            a((DefaultBpmProcessDef)((SubProcessNodeDef)nodeDef).getChildBpmProcessDef(), bpmDefSetting);
         }
      }

   }

   private static List<AbsFlowParse> getFlowParsers() {
      if (CollectionUtil.isNotEmpty(bv)) {
         return bv;
      } else {
         Map<String, AbsFlowParse> map = AppUtil.getImplInstance(AbsFlowParse.class);
         bv = new ArrayList(map.values());
         return bv;
      }
   }

   private static List<AbsNodeParse> getNodeParsers() {
      if (CollectionUtil.isNotEmpty(bw)) {
         return bw;
      } else {
         Map<String, AbsNodeParse> map = AppUtil.getImplInstance(AbsNodeParse.class);
         bw = new ArrayList(map.values());
         return bw;
      }
   }
}
